/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Bill.java
 *
 * Created on 10-Feb-2011, 12:02:31
 */
package iciim;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Vidya
 */
public class Bill extends javax.swing.JFrame {

    Connection con = Main.con;

    /** Creates new form Bill */
    public Bill() {
        initComponents();
        setVisible(true);
        try {
            PreparedStatement ps = con.prepareStatement("Select * from billdet");
            ResultSet r = ps.executeQuery();
            while (r.next()) {
                BillNoCombo.addItem(r.getString("Bill_Id"));

            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        table = new javax.swing.JTable();
        BillNoCombo = new javax.swing.JComboBox();
        BDate = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        total = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "S.No", "TName", "Quantity", "Price/unit", "Total"
            }
        ));
        jScrollPane1.setViewportView(table);

        BillNoCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { " " }));
        BillNoCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BillNoComboActionPerformed(evt);
            }
        });

        jLabel1.setText("Bill Date");

        jLabel2.setText("Total");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(109, 109, 109)
                        .addComponent(BillNoCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 155, Short.MAX_VALUE)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(BDate, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(15, Short.MAX_VALUE)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(310, Short.MAX_VALUE)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(total, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(BillNoCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(42, 42, 42)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(total, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(46, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void BillNoComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BillNoComboActionPerformed
        int i = 0;
        try {
            PreparedStatement st1 = con.prepareStatement("Select Bill_Date from billdet where Bill_Id=?");
            st1.setString(1, (String) BillNoCombo.getSelectedItem());
            ResultSet rs = st1.executeQuery();
            rs.next();
            BDate.setText(rs.getString("Bill_Date"));
            PreparedStatement st2 = con.prepareStatement("select Total from billdet WHERE Bill_Id=?");
            st2.setString(1, (String) BillNoCombo.getSelectedItem());
            ResultSet rs2 = st2.executeQuery();
            rs2.next();
            total.setText(rs2.getString("Total"));
            String t=rs2.getString("Total");
            System.out.println(t);
            PreparedStatement st = con.prepareStatement("select * from billtable WHERE Bill_Id=?");
            st.setString(1, (String) BillNoCombo.getSelectedItem());
            ResultSet rs1 = st.executeQuery();
            DefaultTableModel model = (DefaultTableModel) table.getModel();
            while (rs1.next()) {
                model.addRow(new Object[]{"", "", "", "", ""});
                String sno = rs1.getString("S.No");
                String name = rs1.getString("Type_Name");
                String qty = rs1.getString("Quantity");
                String uprice = rs1.getString("Price/unit");
                String tprice = rs1.getString("Total_Price");
                model.setValueAt(sno, i, 0);
                model.setValueAt(name, i, 1);
                model.setValueAt(qty, i, 2);
                model.setValueAt(uprice, i, 3);
                model.setValueAt(tprice, i, 4);
                i++;
            }
           
        } catch (Exception e) {
        }
        // TODO add your handling code here:
    }//GEN-LAST:event_BillNoComboActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Bill().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField BDate;
    private javax.swing.JComboBox BillNoCombo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable table;
    private javax.swing.JTextField total;
    // End of variables declaration//GEN-END:variables
}
